package com.resifind.www.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resifind.www.constant.ApplicationStatus;
import com.resifind.www.constant.RechargeStatus;
import com.resifind.www.constant.UserBanStatus;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.*;
import com.resifind.www.service.AdminService;
import com.resifind.www.service.MerchantService;
import com.resifind.www.service.impl.AdminServiceImpl;
import com.resifind.www.service.impl.MerchantServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * @author 36069
 */
@SuppressWarnings({"unused", "RedundantThrows"})
@WebServlet("/admin/*")
public class AdminServlet extends BaseServlet {
    private final AdminService adminService = new AdminServiceImpl();
    private final MerchantService merchantService = new MerchantServiceImpl();

    /**
     * 管理员登录
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String password = req.getReader().readLine();
        try {
            Resp<String> loginResp = adminService.login(password);
            req.getSession().setAttribute("admin", password);
            resp.getWriter().write(JSON.toJSONString(loginResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 检查登录状态
     */
    public void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object admin = req.getSession().getAttribute("admin");
        if (admin == null) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(500, "未登录")));
        } else {
            resp.getWriter().write(JSON.toJSONString(Resp.success("已登录")));
        }
    }

    /**
     * 获取所有商户信息
     */
    public void getMerchants(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer currentPage = root.getInteger("currentPage");
        Integer pageSize = root.getInteger("pageSize");
        String tradeName = root.getString("tradeName");
        Resp<PageBean<Merchant>> pageBeanResp = merchantService.queryByPageAndConditionForAdmin(currentPage, pageSize, tradeName);
        resp.getWriter().write(JSON.toJSONString(pageBeanResp));
    }

    /**
     * 获取正在申请的商户信息
     */
    public void getPendingMerchants(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        try {
            Resp<List<Merchant>> listResp = adminService.getPendingMerchants();
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 封禁商户
     */
    public void banMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = merchantService.updateMerchantStatus(merchantId, ApplicationStatus.BANNED);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 解封商户
     */
    public void unbanMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = merchantService.updateMerchantStatus(merchantId, ApplicationStatus.APPROVED);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 批准商户
     */
    public void approveMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = merchantService.updateMerchantStatus(merchantId, ApplicationStatus.APPROVED);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 拒绝商户
     */
    public void rejectMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = merchantService.updateMerchantStatus(merchantId, ApplicationStatus.PENDING);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 审核充值
     */
    public void getPendingRecharges(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        try {
            Resp<List<Recharge>> listResp = adminService.queryRecharge(RechargeStatus.WAITING);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 批准充值
     */
    public void approveRecharge(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer rechargeId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = adminService.updateRechargeStatus(rechargeId, RechargeStatus.APPROVED);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 拒绝充值
     */
    public void rejectRecharge(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer rechargeId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = adminService.updateRechargeStatus(rechargeId, RechargeStatus.REJECTED);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 获取所有用户信息
     */
    public void getClients(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Resp<PageBean<Client>> pageBeanResp = adminService.getClientsByPage(currentPage, pageSize);
        resp.getWriter().write(JSON.toJSONString(pageBeanResp));
    }

    /**
     * 获取用户签到统计信息
     */
    public void getSignInStatistics(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Resp<SignInStatistics> signInStatisticsResp = adminService.getSignInStatistics();
        resp.getWriter().write(JSON.toJSONString(signInStatisticsResp));
    }

    /**
     * 封禁客户
     */
    public void banClient(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = adminService.updateClientStatus(clientId, UserBanStatus.BAN);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 解封客户
     */
    public void unbanClient(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<String> updateResp = adminService.updateClientStatus(clientId, UserBanStatus.NORMAL);
            resp.getWriter().write(JSON.toJSONString(updateResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }
}
